﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;

namespace UserGroupData
{
    public class PresentationTypeRepository : IPresentationTypeRepository
    {
        public PresentationTypeDTO GetPresentationType(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                var meeting = ctx.PresentationTypes.Where(d => d.Id == id);
                var entity = meeting.FirstOrDefault();
                Mapper.CreateMap<PresentationType, PresentationTypeDTO>();
                var dto = Mapper.Map<PresentationType, PresentationTypeDTO>(entity);
                return dto;
            }
        }

        public List<PresentationTypeDTO> GetAllPresentationTypes()
        {
            using (var ctx = new UserGroupEntities())
            {
                var entities = ctx.PresentationTypes.ToList();
                Mapper.CreateMap<PresentationType, PresentationTypeDTO>();
                var dto = Mapper.Map<List<PresentationType>, List<PresentationTypeDTO>>(entities);
                return dto;
            }
        }

        public int AddNewPresentationType(PresentationTypeDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<PresentationTypeDTO, PresentationType>();
                var entity = Mapper.Map<PresentationTypeDTO, PresentationType>(dto);
                ctx.AddToPresentationTypes(entity);
                ctx.SaveChanges();
                return entity.Id;
            }
        }

        public void UpdatePresentationType(PresentationTypeDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<PresentationTypeDTO, PresentationType>();
                var entity = Mapper.Map<PresentationTypeDTO, PresentationType>(dto);
                ctx.ApplyCurrentValues("PresentationTypes", entity);
                ctx.SaveChanges();
            }
        }

        public void DeletePresentationTypeDTO(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                PresentationType dbDept = ctx.PresentationTypes.First(d => d.Id == id);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
